package com.indigon.parkStory.view
{
	import com.indigon.parkStory.data.levels.LevelData;
	import com.indigon.parkStory.model.GameEvent;
	import com.indigon.parkStory.model.ModelMain;
	import com.indigon.parkStory.model.objects.LillyModel;
	import com.indigon.parkStory.model.objects.WaveModel;
	import com.indigon.parkStory.ui.Screen;
	import com.indigon.parkStory.view.objects.FrogView;
	import com.indigon.parkStory.view.objects.LillyView;
	import com.indigon.parkStory.view.objects.ObjectView;
	import com.indigon.parkStory.view.objects.WaveView;
	import com.indigon.parkStory.view.objects.text.FloatingText;
	import com.indigon.parkStory.view.objects.text.ScoreFloatingText;
	import com.indigon.parkStory.view.objects.text.StarFloatingText;
	import com.indigon.parkStory.view.objects.ui.RangeView;
	import com.indigon.parkStory.view.objects.ui.ScoreView;
	
	import flash.display.Stage;
	import flash.geom.Point;
	import flash.utils.Dictionary;

	public class ViewMain extends Screen
	{
		private var _model    : ModelMain;
		private var _objects  : Dictionary;
		private var _range    : RangeView;
		private var _ui 	  : ScoreView;
		private var _frog 	  : FrogView;
		
		private var _stage	  : Stage;
		
		public static var CENTER_OFFSET : Point;
		
		public function ViewMain(model:ModelMain)
		{
			this._model = model;
		}
		
		public function init(centerOffset:Point, stage:Stage):void
		{
			this._stage = stage;
			CENTER_OFFSET = centerOffset;
			
			this.graphics.beginFill(0x335671, 1);
			this.graphics.drawRect(0, 0, stage.stageWidth, stage.stageHeight);
			this.graphics.endFill();
		}
		
		public function reset():void
		{
			for each (var object : ObjectView in _objects)
			{
				object.uninit();
			}
			_objects = null;
			
			_range   = null;
			_frog    = null;
			_ui      = null;
		}
		
		public function config(level:LevelData):void
		{
			_objects = new Dictionary();
			
			for each (var lilly : LillyModel in _model.lillies)
			{
				var lillyView : LillyView = new LillyView(lilly);
				lillyView.set(this, lilly.data.pos.x + CENTER_OFFSET.x, lilly.data.pos.y + CENTER_OFFSET.y);
				_objects[lilly] = lillyView;
			}
			
			_frog = new FrogView(_model.frog);
			_frog.set(this, _model.frog.lilly.data.pos.x + CENTER_OFFSET.x, _model.frog.lilly.data.pos.y + CENTER_OFFSET.y);
			_objects[_model.frog] = _frog;
			
			this._range = new RangeView(_model.frog);			
			_range.set(this, _model.frog.lilly.data.pos.x + CENTER_OFFSET.x, _model.frog.lilly.data.pos.y + CENTER_OFFSET.y, 0, numChildren);
			_objects[RangeView.ID] = _range;
			
			this._ui = new ScoreView(_model.score);
			_ui.set(this, _stage.stageWidth, 0, 0, numChildren);
			_objects[ScoreView.ID] = _ui;
		}
		
		public function start():void
		{
			_range.show();
		}
		
		public function end():void
		{
			_ui.updateStats();
		}
		
		public function update():void
		{
			// objects.
			for (var object : Object in _objects)
			{
				var viewObject : ObjectView = _objects[object];
				if (viewObject.removed)
				{
					delete _objects[object];
				}
				else
				{
					viewObject.update();
				}
			}

			// events.
			var events : Vector.<GameEvent> = _model.events;
			for each (var event : GameEvent in events)
			{
				switch (event.type)
				{
					case GameEvent.LILLY_SINGLE 	: 
					{
						LillyView(_objects[event.object]).singleDrown();
						break;
					}
					case GameEvent.LILLY_FADE  		: 
					{
						LillyView(_objects[event.object]).fadeDrown();
						break;
					}
					case GameEvent.JUMP_START 		: 
					{
						_range.hide();
						_frog.jump();
						LillyView(_objects[event.object]).jumpOff();
						break;
					}
					case GameEvent.JUMP_SUCCESS 	: 
					{
						_range.show();
						_frog.land();
						LillyView(_objects[event.object]).jumpOn();
						break;
					}
					case GameEvent.WAVE_GEN 		: 
					{
						var waveModel : WaveModel = WaveModel(event.object);
						var waveView : WaveView = new WaveView(waveModel);
						waveView.set(this, waveModel.pos.x + CENTER_OFFSET.x, waveModel.pos.y + CENTER_OFFSET.y);
						_objects[waveModel] = waveView;
						break;
					}
					case GameEvent.STAR_COLLECTED 	: 
					{
						_ui.updateStats();
						var starLilly : LillyView = LillyView(_objects[event.object]);
						starLilly.collectStar();
						var starText : StarFloatingText = new StarFloatingText(_model.score.collectedStars);
						starText.set(this, starLilly.graphic.x + FloatingText.POS_DIFF.x, starLilly.graphic.y + FloatingText.POS_DIFF.y, 0, numChildren);
						break;
					}
					case GameEvent.GOAL_COLLECTED 	: 
					{
						_ui.updateStats();
						var goalLilly : LillyView = LillyView(_objects[event.object]);
						goalLilly.collectGoal();
						var comboText : ScoreFloatingText = new ScoreFloatingText(_model.score.combo);
						comboText.set(this, goalLilly.graphic.x + FloatingText.POS_DIFF.x, goalLilly.graphic.y + FloatingText.POS_DIFF.y, 0, numChildren);
						break;
					}
					case GameEvent.WIN_LEVEL		:
					{
						_range.hide();
						break;
					}
					case GameEvent.LOSE_LEVEL		:
					{
						_frog.drown();
						_range.hide();
						break;
					}
				}
			}
		}
	}
}